import requests
import time
import random
import csv
import json
import pandas as pd
from django.http import JsonResponse
from common.error import *
from django.conf import settings
import os


def get_citygeo(entity, type, faddress):
    try:
        if type == 'geo':
            temp_file = faddress + '/file/geo/'
            file = temp_file + '城市模版.csv'
            result = '/file/geo/' + '城市模版.csv'
        elif type == 'common':
            temp_file = faddress + '/file/common/'
            file = temp_file + '通用模版.csv'
            result = '/file/common/' + '通用模版.csv'
        if not os.path.exists(temp_file):
            os.makedirs(temp_file)
        if not os.path.exists(file):
            with open(file, 'w+') as f:
                csv_write = csv.writer(f)
                csv_head = ['entity', 'domain', 'desc', 'avg', 'tag']
                csv_write.writerow(csv_head)
        time.sleep(random.uniform(1, 3))  # 避免爬取速度过快
        url = 'https://api.ownthink.com/kg/knowledge?entity=' + entity
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = 'utf-8'
        raw_data = json.loads(r.content)
        data = raw_data['data']
        new_avp = {x[0]: x[1] for x in data["avp"]}
        df = pd.read_csv(file)
        print(df)
        s = pd.Series([data["entity"], data["domain"], data["desc"], new_avp, data["tag"]], index=df.columns)
        df = df.append(s, ignore_index=True)
        df = df.drop_duplicates(subset=['entity'], keep='first', inplace=False)
        print(df)
        df.to_csv(file, index=False, encoding='utf-8')
        info = {
            'successful': True,
            'data': result,
            'code': ERROR_CODE_OPERATION_SUCCESS,
            'message': '数据获取成功'
        }
        return JsonResponse(info, json_dumps_params={'ensure_ascii': False})
    except Exception as e:
        info = {
            'successful': False,
            'data': e,
            'code': ERROR_CODE_PARTNER_ERROR,
            'message': '爬取脚本出错'
        }
        return JsonResponse(info, json_dumps_params={'ensure_ascii': False})
    return JsonResponse(info, json_dumps_params={'ensure_ascii': False})


if __name__ == '__main__':
    get_citygeo('烟台', 'common', 'faddress')
